package com.link510.aitools.activity;

import android.Manifest;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.helper.Network2kUtils;
import com.link510.aitools.core.helper.UnixTimeHelper;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.CustomWaitDialogUtils;
import com.link510.aitools.services.Wifi2Manager;
import com.orhanobut.hawk.Hawk;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;
import pub.devrel.easypermissions.EasyPermissions;

public class LoginActivity extends BaseAcitivity {

    @BindView(R.id.login_button)
    Button button;

    @BindView(R.id.account_edittext)
    EditText accountEdittext;

    @BindView(R.id.password_edittext)
    EditText passwordEdittext;

    /**
     * 随便赋值的一个唯一标识码
     */
    public static final int REQUEST_CODE = 1001;

    private CustomWaitDialogUtils customWaitDialogUtils;

    private static final String PERMISSION_KEYS = "permission_keys";

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        //将结果传入EasyPermissions中
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    /**
     * 权限判断和申请
     *
     * @param perms       权限
     * @param des         说明
     * @param requestCode 状态
     */
    public void requestPermission(String[] perms, String des, int requestCode) {

        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, des, requestCode,
                    perms);
            Hawk.put(PERMISSION_KEYS, UnixTimeHelper.getInstance().getUnixTimeStamp().toString());
        }

    }

    public void checkMinePermission() {


        String keys = Hawk.get(PERMISSION_KEYS, null);

        if (Strings.isNullOrEmpty(keys)) {
            String[] manifest = new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.CHANGE_WIFI_STATE,
                    Manifest.permission.CHANGE_NETWORK_STATE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_SETTINGS,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.CAMERA,
                    Manifest.permission.WAKE_LOCK,

            };


            requestPermission(manifest, "权限申请", REQUEST_CODE);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.FIRST, 1, 1001, "WiFi自动连接");
        menu.add(Menu.FIRST, 1, 1002, "WiFi扫码");
        return super.onCreateOptionsMenu(menu);
    }


    // 菜单的监听方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if (item.getItemId() == 1) {
            switch (item.getOrder()) {
                case 1001: {

                    boolean f = NetworkUtils.isWifiConnected();

                    if (f) {
                        ToastUtils.showShort("Wi-Fi已打开.");

                    } else {
                        ToastUtils.showShort("正在打开了Wi-Fi.");
                        NetworkUtils.setWifiEnabled(true);

                        WifiConfiguration config = Wifi2Manager.getInstance().CreateWifiInfo("YK-SVT-2000007", "1234567890", 3);

                        boolean b = Wifi2Manager.getInstance().addNetwork(config);

                        if (b) {
                            ToastUtils.showShort("Wi-Fi连接成功");
                        } else {

                            ToastUtils.showShort("Wi-Fi连接失败");
                        }
                    }

                }
                break;
                case 1002: {
                    Intent intent = new Intent(LoginActivity.this, ScanWifiActivity.class);
                    LoginActivity.this.startActivity(intent);
                }
                break;
                default:


            }
        }

        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        customWaitDialogUtils = new CustomWaitDialogUtils(LoginActivity.this);


        checkMinePermission();
    }

    @OnLongClick(R.id.welcome_textview)
    public void login_button_long_click() {
        Intent intent = new Intent(LoginActivity.this, TestActivity.class);
        startActivity(intent);

    }

    @OnClick(R.id.login_button)
    public void login_button_click() {


        customWaitDialogUtils.show("登录中...");

        try {

            String account = accountEdittext.getText().toString().trim();

            if (Strings.isNullOrEmpty(account)) {
                ToastUtils.showShort("账号不能为空");
                return;
            }


            String password = passwordEdittext.getText().toString().trim();

            if (Strings.isNullOrEmpty(password)) {
                ToastUtils.showShort("密码不能为空");
                return;
            }

            if (!("incar".equals(account))) {
                ToastUtils.showShort("账号错误");
                return;
            }

            customWaitDialogUtils.show("账号校验成功...");


            if (!("Incar123456".equals(password))) {
                ToastUtils.showShort("密码错误");
                return;
            }

            customWaitDialogUtils.show("密码校验成功...");

            ToastUtils.showShort("用户登录成功");

            customWaitDialogUtils.close();

            Intent intent = new Intent(LoginActivity.this, Login2Activity.class);

            startActivity(intent);
            finish();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        customWaitDialogUtils.dismiss();
    }
}
